package nexd.com.employ.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.OnClick;
import cn.nexd.framework.util.Logger;
import cn.nexd.map.rendering.SVGMapViewListener;
import cn.nexd.map.rendering.core.SVGMapBaseOverlay;
import cn.nexd.map.rendering.core.componet.SVGMapView;
import nexd.com.employ.R;
import nexd.com.employ.presenter.NexdMapPresenter;
import nexd.com.employ.ui.iView.INexdMapView;


public class NexdMapActivity extends BaseActivity<NexdMapPresenter> implements INexdMapView, SVGMapViewListener {


    @Bind(R.id.svg_map)
    SVGMapView nexdMap;

    @Bind(R.id.iv_add)
    ImageButton add;

    @Bind(R.id.iv_sub)
    ImageButton sub;

    @Bind(R.id.tv_other)
    TextView other;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tl_tv_title)
    TextView tvTitle;
    @Bind(R.id.tl_tv_about)
    TextView tvAbout;

    @Override
    protected int getLayoutResId() {

        return R.layout.activity_map;
    }

    @Override
    protected void initPresenter() {
        presenter = new NexdMapPresenter(this, this);
        presenter.init();
    }

    @Override
    public void initView() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        tvTitle.setText("昌葵多人位置监控系统");
        nexdMap.registerMapViewListener(this);
        presenter.setNexdMap(nexdMap);
        presenter.loadMap();
    }

    @Override
    public void onMapLoadComplete(long l, final String s) {
//        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
        Log.d(TAG, "onMapLoadComplete: " + s);
        presenter.requestLocation();
//            }
//        });
    }

    @Override
    public void onMapLoadError() {
        Log.d(TAG, "onMapLoadError: 地图加载失败");
    }

    @Override
    public void onGetCurrentMap(Bitmap bitmap) {

    }

    @Override
    public void addMapOverly(final SVGMapBaseOverlay overlay, final int type) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                nexdMap.addOverlay(overlay, type);
            }
        });
    }

    @Override
    public void loadMap(String svgPath) {
        try {
            Log.d(TAG, "loadMap: " + svgPath);
            nexdMap.loadMap(new FileInputStream(new File(svgPath)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void updateOthers(final String others) {
        other.setText(new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date()) + "    总人数: " + others);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        nexdMap.destroyDrawingCache();
        nexdMap.onDestroy();
    }


    @OnClick({R.id.iv_add, R.id.iv_sub, R.id.tl_tv_about})
    public void onClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_add:
                Logger.debug(TAG, "add press");
                float add = nexdMap.getCurrentZoomValue() + 5;

                if (add > nexdMap.getMaxZoomValue()) {
                    return;
                }
                nexdMap.setCurrentZoomValue(add);
                break;
            case R.id.iv_sub:
                Logger.debug(TAG, "sub press");
                float sub = nexdMap.getCurrentZoomValue() - 5;
                if (sub < nexdMap.getMinZoomValue()) {
                    return;
                }
                nexdMap.setCurrentZoomValue(sub);
                break;
            case R.id.tl_tv_about:
                Intent intent = new Intent(this, AboutUsActivity.class);
                startActivity(intent);
                break;
        }
    }

}
